package RecursiveSearcher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @ClassName LC90
 * @Description
 * @Author ZJX
 * @Date 2024/10/20 20:46
 * @Version 1.0
 */
public class LC90 {
    List<List<Integer>> ret = new LinkedList<>();
    List<Integer> path = new LinkedList<>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        ret = new LinkedList<>();
        path = new LinkedList<>();
        Arrays.sort(nums);
       dfs(nums,0);

       return ret;
    }

    private void dfs(int[] nums, int pos) {
        ret.add(new LinkedList<>(path));
        for (int i = pos; i < nums.length; i++) {

            if (i > pos && nums[i] == nums[i - 1]) {
                continue;  
            }

            // 选择当前元素，加入路径
            path.add(nums[i]);
            // 递归进入下一层
            dfs(nums, i + 1);
            // 回溯，移除最后一个选择的元素
            path.remove(path.size() - 1);
        }
    }
}
